<?php
/**
 * admin service file Created by PhpStorm.
 * User: wumengmeng
 * Date: 2020/05/11
 * Time: 16:24
 */

namespace Modules\Admin\Service;

use HiCommon\Repository\HandbookRepository;
use HiCommon\Repository\HandbookUpkeepTypeRepository;

class HandbookService
{

    /**
     * 获取所有权限
     *
     * @return array
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function get_all()
    {
        try {
            $arr_option = ['order' => ['key' => 'id', 'value' => 'asc']];
            $data = HandbookRepository::get_all($arr_option)->toarray();
            return hello_success('成功', $data);
        } catch (\Exception $exception) {
            return hello_error('失败', [], $exception->getMessage());
        }
    }

    /**
     * 获取分页数据
     *
     * @return array
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function get_list()
    {
        try {
            $arr_option = [
                'order' => ['key' => 'id', 'value' => 'desc'],
                'with' => ['get_handbook_upkeep_type']
            ];
            $data = HandbookRepository::base_get_list($arr_option);
            $data = list_page_data($data);
            foreach ($data['data'] as $key => &$vale) {
                $hand = array_column($vale['get_handbook_upkeep_type'], 'get_upkeep_type');
                $vale['hand'] = implode('，', array_column($hand, 'title'));
            }
            return hello_success('成功', $data);
        } catch (\Exception $exception) {
            return hello_error('失败', [], $exception->getMessage());
        }
    }

    /**
     * 添加一条数据
     *
     * @param $arr_input
     *
     * @return array|\Illuminate\Http\RedirectResponse
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function add_one($arr_input)
    {
        $arr_input = hi_array_del_null($arr_input);
        try {
            $data = ['km' => $arr_input['km'], 'month' => $arr_input['month']];
            if (empty($arr_input['upkeep'])) {
                return hello_error('请勾选保养项');
            }
            //添加
            $res = HandbookRepository::base_add_one($data)->toarray();

            if ($res && $arr_input['upkeep']) {
                $book = [];
                foreach ($arr_input['upkeep'] as $item) {
                    $book[] = [
                        'handbook_id' => $res['id'],
                        'upkeep_type_id' => $item
                    ];
                }
                if (HandbookUpkeepTypeRepository::base_add_many($book)) {
                    return hello_success('成功', $res);
                }
            }

            return hello_error('添加失败');
        } catch (\Exception $exception) {
            return hello_error('失败', [], $exception->getMessage());
        }
    }

    /**
     * 查询一条数据
     *
     * @param int $n_id
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function get_one(int $n_id)
    {
        try {
            $n_id = (int)$n_id;
            if ($n_id <= 0) {
                hello_error('数据ID不能为空');
            }

            //查询
            $arr_option = ['with' => ['get_handbook_upkeep_type']];
            $res = HandbookRepository::base_find_one($n_id, $arr_option)->toarray();
            $res['upkeep'] = array_column($res['get_handbook_upkeep_type'], 'upkeep_type_id');
            if (empty($res)) {
                return hello_error('查询失败');
            }

            return hello_success('查询成功', $res);
        } catch (\Exception $exception) {
            return hello_error('失败', [], $exception->getMessage());
        }
    }

    /**
     * 修改一条数据
     *
     * @param array $arr_input
     *
     * @return array|\Illuminate\Http\RedirectResponse
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function edit_one(array $arr_input)
    {
        $arr_input = hi_array_del_null($arr_input);
        try {
            $id = (int)$arr_input['id'];
            if ($id <= 0) {
                return hello_error('数据id不能为空');
            }
            $data = ['id' => $arr_input['id'], 'km' => $arr_input['km'], 'month' => $arr_input['month']];

            if (empty($arr_input['upkeep'])) {
                return hello_error('请勾选保养项');
            }

            $res = HandbookRepository::base_edit_one($data);
            if ($res && $arr_input['upkeep']) {

                HandbookUpkeepTypeRepository::base_delete_by_where(['handbook_id' => $id], true);
                $book = [];
                foreach ($arr_input['upkeep'] as $item) {
                    $book[] = [
                        'handbook_id' => $id,
                        'upkeep_type_id' => $item
                    ];
                }
                if (HandbookUpkeepTypeRepository::base_add_many($book)) {
                    return hello_success('成功', $res);
                }
            }

            return hello_error('操作失败');

        } catch (\Exception $exception) {
            return hello_error('失败', [], $exception->getMessage());
        }
    }

    /**
     * 修改单个字段
     *
     * @param $arr_input
     *
     * @return array
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function change_one_state($arr_input)
    {
        try {
            $arr_input['id'] = (int)$arr_input['id'];
            if ($arr_input['id'] <= 0) {
                return hello_error('数据id不能为空');
            }

            $res = HandbookRepository::edit_one($arr_input);
            if ($res === false) {
                return hello_error('操作失败');
            }
            return hello_success('成功');
        } catch (\Exception $exception) {
            return hello_error('失败', [], $exception->getMessage());
        }
    }

    /**
     * 删除一条数据
     *
     * @param int $n_id
     *
     * @return array|\Illuminate\Http\RedirectResponse
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function delete_one($n_id)
    {
        try {
            if ($n_id <= 0) {
                return hello_error('数据id不能为空');
            }

            $res = HandbookRepository::delete($n_id, true);
            if ($res === false) {
                return hello_error('操作失败');
            }
            return hello_success('成功');

        } catch (\Exception $exception) {
            return hello_error('失败', [], $exception->getMessage());
        }
    }
}